//
//  BIDPersonalInfoViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/17.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDPersonalInfoViewController.h"
#import "BIDHeaderForPersonalView.h"
#import "BIDSettingsViewController.h"
#import "BIDMyHouseViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface BIDPersonalInfoViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_dataSourceArr;
    BIDHeaderForPersonalView *_headerView;
}
@end

@implementation BIDPersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _dataSourceArr = @[@{@"title":@"我的房产", @"imgName":@"my_house.png", @"className":@"BIDMyHouseViewController", @"isNeedXib":@0},
                       @{@"title":@"我的消息", @"imgName":@"my_msg.png", @"className":@"BIDMyMessageListViewController", @"isNeedXib":@0},
                       @{@"title":@"我的报修", @"imgName":@"my_repair.png", @"className":@"BIDRepairKindForOwnerViewController", @"isNeedXib":@1},
                       @{@"title":@"我的发布", @"imgName":@"my_publish.png", @"className":@"BIDOwnPublishViewController", @"isNeedXib":@1},
                       @{@"title":@"我的店铺", @"imgName":@"my_store.png", @"className":@"BIDMyStoreGoodsListViewController", @"isNeedXib":@0},];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    parent.title = @"我的";
    parent.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"settings.png"] style:UIBarButtonItemStylePlain target:self action:@selector(settingsHandler)];
    //
    BIDHeaderForPersonalView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"BIDHeaderForPersonalView" owner:self options:nil] lastObject];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat height = screenSize.width*214/320;
    headerView.frame = CGRectMake(0, 0, screenSize.width, height);
    headerView.heightForImgViewConstraint.constant = 70*height / 214;
    headerView.topForImgViewConstraint.constant = 29*height / 214;
    [headerView layoutIfNeeded];
    _tableView.tableHeaderView = headerView;
    _headerView = headerView;
    _headerView.nameLabel.text = [AppDelegate getUserDetailInfo][@"nickName"];
    //
    _tableView.tableFooterView = [[UIView alloc] init];
    [_tableView setBackgroundColor:RGBACOLOR(244.f, 244.f, 245.f, 1.f)];
    //
    [self fetchPortrait];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self fetchPortrait];
    _headerView.nameLabel.text = [AppDelegate getUserDetailInfo][@"nickName"];
}

- (void)settingsHandler
{
    BIDSettingsViewController *vc = [[BIDSettingsViewController alloc] initWithNibName:@"BIDSettingsViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)fetchPortrait
{
    NSDictionary *userDetail = [AppDelegate getUserDetailInfo];
    NSString *strAvatarPath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], userDetail[@"photo"]];
    [_headerView.avatarImgView setImageWithURL:[NSURL URLWithString:strAvatarPath] placeholderImage:[UIImage imageNamed:AVATAR_PLACEHOLDER]];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dic = _dataSourceArr[row];
    cell.textLabel.text = dic[@"title"];
    NSString *strImgName = dic[@"imgName"];
    cell.imageView.image = [UIImage imageNamed:strImgName];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _dataSourceArr[indexPath.row];
    NSString *strClassName = dic[@"className"];
    NSNumber *value = dic[@"isNeedXib"];
    UIViewController *vc;
    if([value intValue] == 0)
    {
        vc = [[NSClassFromString(strClassName) alloc] init];
    }
    else
    {
        vc = [[NSClassFromString(strClassName) alloc] initWithNibName:strClassName bundle:nil];
    }
    [self.navigationController pushViewController:vc animated:YES];
}



@end
